// StatusWindow.cpp : implementation file
//

#include "stdafx.h"
#include <sstream>

#include "StatusWindow.h"
#include "WinMacroDialog2.h"
#include <iomanip>

extern WinMacroDialog2 * dialog_ptr;
extern HINSTANCE hinstance;

// StatusWindow dialog

IMPLEMENT_DYNAMIC(StatusWindow, CDialog)

StatusWindow::StatusWindow(CWnd* pParent /*=NULL*/)
	: CDialog(StatusWindow::IDD, pParent)
{
	m_winmacroLabelFont.CreatePointFont(150, "Tahoma");
	m_otherTextFont.CreatePointFont(100, "Tahoma");

	Status status = dialog_ptr->GetStatus();

	m_elapsedTimeFont.CreatePointFont(status == Recording ? 160 : 120, "Tahoma");
}

StatusWindow::~StatusWindow()
{
}

void StatusWindow::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BOOL StatusWindow::OnInitDialog()
{
	SetLabelFonts();
	SetLayeredWindowAttributes(0, (BYTE)(0.7 * 255), LWA_ALPHA);
	MoveWindowToTopRightCornerOfDesktop();

	GetDlgItem(IDC_PLAYBACK_INFO_LABEL)->ShowWindow( dialog_ptr->GetStatus() == PlayingBack ? SW_SHOW : SW_HIDE); 

	SetTimer(REPAINT_TIMER, 1000, NULL);

	UpdateUI();

	return TRUE;
}

void StatusWindow::OnTimer(UINT_PTR timerId)
{
	UpdateUI();
}

void StatusWindow::MoveWindowToTopRightCornerOfDesktop()
{
    RECT currentWindowRect;
	GetWindowRect(&currentWindowRect);
	
	CWnd *pDesktopWindow = GetDesktopWindow();
	WINDOWINFO desktopWindowInfo;
	::GetWindowInfo(pDesktopWindow->m_hWnd, &desktopWindowInfo);
	
	SetWindowPos(&wndTopMost, desktopWindowInfo.rcClient.right - (currentWindowRect.right - currentWindowRect.left), 0, 0, 0, SWP_NOSIZE);
}

void StatusWindow::SetLabelFonts()
{
	CStatic *pWinMacroLabel = (CStatic *)GetDlgItem(IDC_WINMACRO_LABEL);
	pWinMacroLabel->SetFont(&m_winmacroLabelFont);

	CStatic *pStatusLabel = (CStatic *)GetDlgItem(IDC_STATUS_LABEL);
	CStatic *pElapsedTimeLabel = (CStatic *)GetDlgItem(IDC_ELAPSED_TIME_LABEL);
	CStatic *pPlaybackProgressLabel = (CStatic *)GetDlgItem(IDC_ELAPSED_TIME_LABEL);

	pStatusLabel->SetFont(&m_otherTextFont);
	pPlaybackProgressLabel->SetFont(&m_otherTextFont);
	pElapsedTimeLabel->SetFont(&m_elapsedTimeFont);
}

BEGIN_MESSAGE_MAP(StatusWindow, CDialog)
	ON_WM_TIMER()	
END_MESSAGE_MAP()

std::string StatusWindow::GetStatusText()
{
	Status status = dialog_ptr->GetStatus();
	std::string statusText = "";
	if (status == Idle)
	{
		statusText = "Idle";
	}
	else
	{
		statusText = (status == Recording ? "Recording" : "Playing back");
	}

	return statusText;
}

std::string StatusWindow::GetElapsedTimeText()
{
	DWORD elapsedTime = GetTickCount() - dialog_ptr->GetActionStartTime();

	int seconds = elapsedTime / 1000;
	int minutes = seconds / 60;
	int hours = minutes / 24;

	std::ostringstream ostr;
	ostr
		<< std::setfill('0') << std::setw(2) << hours << ":" 
		<< std::setfill('0') << std::setw(2) << minutes % 60 << ":" 
		<< std::setfill('0') << std::setw(2) << seconds % 60;

	return ostr.str();

}

void StatusWindow::UpdateUI()
{
	SetDlgItemText(IDC_STATUS_LABEL, GetStatusText().c_str());
	SetDlgItemText(IDC_ELAPSED_TIME_LABEL, GetElapsedTimeText().c_str());

	if (dialog_ptr->GetStatus() == PlayingBack)
	{
		SetDlgItemText(IDC_PLAYBACK_INFO_LABEL, GetPlaybackProgressText().c_str());
	}
}

std::string StatusWindow::GetPlaybackProgressText()
{
	PLAYBACKPROGRESSINFO progress_info = dialog_ptr->GetPlaybackProgressInfo();

	std::ostringstream ostr;
	ostr << "Message " << progress_info.current_message_number << " of "
		<< progress_info.total_number_of_messages << std::endl;

	if (progress_info.total_number_of_iterations > 1)
	{
		ostr << "Iteration " << progress_info.current_iteration_number + 1 << " of " << progress_info.total_number_of_iterations + 1;
	}

	return ostr.str();

}